#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Solution {
public:
    int maximumDifference(vector<int>& nums) {
        int ret = -1;
        bool isFind = false;
        int len = nums.size();
        int minNum = nums[0];
        int maxNum = nums[0];

        for (int idx = 1; idx < len; idx++) {

            if (isFind == true) {
                if (nums[idx] < minNum) {
                    isFind = false;
                    minNum = nums[idx];
                    maxNum = nums[idx];
                } else {
                    maxNum = max(maxNum, nums[idx]);
                    ret = max(ret, maxNum - minNum);
                }   
            } else {
                if (nums[idx] > minNum) {
                    isFind = true;
                    maxNum = nums[idx];
                    ret = max(ret, maxNum - minNum);
                } else {
                    minNum = min(minNum, nums[idx]);
                }
            }

        }
        return ret;
    }
};


int main()
{
    Solution s;
    vector<int> vt{87,68,91,86,58,63,43,98,6,40};
    int ret = s.maximumDifference(vt);
    cout << ret << endl;

    return 0;
}